package com.rtx.wms.std.sku.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rtx.api.utils.MultipleDataSource;
import com.rtx.common.HttpServletRequestUtil;
import com.rtx.common.JaxbObjectAndXmlUtil;
import com.rtx.model.dto.edi.EdiInnerRequestDTO;
import com.rtx.wms.std.asn.bean.ApiReceipt;
import com.rtx.wms.std.asn.bean.ApiReceiptdetail;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.ctclass.ClassHolder;
import com.rtx.wms.std.qm.bean.RtxQMResponse;
import com.rtx.wms.std.sku.bean.dto.SkuDTO;
import com.rtx.wms.std.sku.bean.po.Sku;
import com.rtx.wms.std.sku.mapper.SkuMapper;
import com.rtx.wms.std.sku.service.SkuService;

import io.swagger.annotations.Api;

@Api(tags = {"商品"})
@RestController
@RequestMapping("/sku")
public class SkuController {
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	SkuService skuService;
	
	@Autowired
	SkuMapper skuMapper;
	

	@PostMapping(value = "create")
	public String skuCreate(HttpServletRequest request) {
		RtxQMResponse res = new RtxQMResponse();
		try {
	   		String requestStr = HttpServletRequestUtil.getHttpBody(request);
    		EdiInnerRequestDTO requestDTO = JSON.parseObject(requestStr, EdiInnerRequestDTO.class);
    		
			SkuDTO dto = JSON.parseObject(requestDTO.getTagetjson(),SkuDTO.class);
			
			Sku po =  skuService.dtoConvertToPo(dto);
			
			MultipleDataSource.setDataSourceKey("enterprise");
			QueryWrapper<Sku> qw = new QueryWrapper<Sku>();
			qw.eq("sku", po.getSku());
			qw.eq("storerkey", po.getStorerkey());
			List<Sku> list =  skuService.list(qw);
			if(list.size()>0) {
				for(Sku sku:list) {
					UpdateWrapper<Sku> where = new UpdateWrapper<Sku>();
					where.eq("sku", sku.getSku());
					where.eq("storerkey", sku.getStorerkey());
					po.setEdiflag("P");
					skuService.update(po, where);
				}
			}else {
				skuService.save(po);
			}
			MultipleDataSource.ClearDataSourceKey();
			res.setSuccess("success");
		} catch (Exception e) {
			log.error("商品创建异常-",e);
			res.setFailure(e.getMessage());
		}

		return JSON.toJSONString(res);
	}
	
	
	@PostMapping(value = "create/config")
	public String skuCreateByConfigField(HttpServletRequest request) {
		RtxQMResponse res = new RtxQMResponse();
		try {
	   		String requestStr = HttpServletRequestUtil.getHttpBody(request);
    		EdiInnerRequestDTO requestDTO = JSON.parseObject(requestStr, EdiInnerRequestDTO.class);
    		
    		Sku po  = addExternalColumns(requestDTO.getTagetjson(),requestDTO);
			//wms可配置
    		skuService.addConfigSku(WmsConstant.ENTERPRISE,po);
    		
			res.setSuccess("success");
		} catch (Exception e) {
			log.error("商品创建异常-",e);
			res.setFailure(e.getMessage());
		}

		return JSON.toJSONString(res);
	}
	
    public Sku addExternalColumns(String targetJson,EdiInnerRequestDTO dto){
    	Sku head = null;
    	try {
        	String apiid =  dto.getConfig().getTargetApi().getId()+"";
        	Map<String,Class> map = ClassHolder.apiconfigClassMap.get(apiid);
        	
        	if(map != null && map.get("main") != null) {
        		Class headclass =map.get("main");
        		head =  (Sku) JSON.parseObject(targetJson,headclass);
        	}else {
        		head = JSON.parseObject(targetJson,Sku.class);
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}

    	return head;
    }
}
